package deployment

import (
	"context"
	"k8s.io/api/apps/v1"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8sapi/lib"
)

//此方法 废了
func GetPodsByDep(ns string,dep *v1.Deployment) []*Pod{
	 ctx:=context.Background()
	listopt:=metav1.ListOptions{
		 LabelSelector:GetRsLableByDeployment(dep),
	}
	list,err:=lib.K8sClient.CoreV1().Pods(ns).List(ctx,listopt)
	if err!=nil{
		panic(err.Error())
	}
	pods:=make([]*Pod,len(list.Items))
	for i,pod:=range list.Items{
		pods[i]=&Pod{
			Name:pod.Name, //获取 pod名称
			Images:GetImagesByPod(pod.Spec.Containers),// 获取pod镜像
			NodeName:pod.Spec.NodeName,//所属节点
			CreateTime:pod.CreationTimestamp.Format("2006-01-02 15:04:05"),//创建时间

		}
	}

	return pods

}
//获取deployment详细  这块还是调用的API
func GetDeployment(ns string,name string ) *Deployment  {
	ctx:=context.Background()
	getopt:=metav1.GetOptions{}
	dep,err:=lib.K8sClient.AppsV1().Deployments(ns).Get(ctx,name,getopt)
	lib.CheckError(err)

	return &Deployment{
		Name:dep.Name,
		NameSpace:dep.Namespace,
		Images:GetImages(*dep),
		CreateTime:dep.CreationTimestamp.Format("2006-01-02 15:03:04"),
	//	Pods:GetPodsByDep(ns,dep),
		Replicas:[3]int32{dep.Status.Replicas,dep.Status.AvailableReplicas,dep.Status.UnavailableReplicas},

	}
}
